package com.tanhua.server.service;

import com.tanhua.domain.db.*;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVo;

import com.tanhua.domain.vo.UserInfoVoAge;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SettingService {
    @Reference
    private SettingApi settingApi;
    @Reference
    private QuestionApi questionApi;
    @Reference
    private BlackListApi blackListApi;
    @Reference
    private UserInfoApi userInfoApi;


    public SettingsVo querySettings() {
        //获取用户登入数据
        User loginUser = UserHolder.getUser();
        //通过用户id查询陌生人问题设置
        Question question = questionApi.findByUserId(loginUser.getId());
        //查询用户通知设置
        Settings settings = settingApi.findByUserId(loginUser.getId());
        //拼接为pojo对象返回
        SettingsVo settingsVo = new SettingsVo();
        settingsVo.setPhone(loginUser.getMobile());
        settingsVo.setStrangerQuestion(null != question ? question.getTxt() : "你想加我嘛？");
        //如果setting为空则不需要设置
        if (null != settings) {
            BeanUtils.copyProperties(settings, settingsVo);
        }
        return settingsVo;
    }

    /**
     * 用户通知设置
     *
     * @param vo
     */
    public void SetSettings(SettingsVo vo) {
        //获取登入用户id
        Long userId = UserHolder.getUserId();
        //vo转换成pojo
        Settings settings = new Settings();
        BeanUtils.copyProperties(vo, settings);
        settings.setUserId(userId);
        //调用Api保存
        settingApi.setSettings(settings);
    }

    /**
     * 黑名单列表
     *
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult<UserInfoVoAge> blackListPage(Long page, Long pagesize) {
        //获取登入用户id
        Long loginUSerId = UserHolder.getUserId();
        //通过id查询黑名单列表
       PageResult pageResult= blackListApi.findPageByUserId(loginUSerId,page,pagesize);
        //取出分页结果集
      List<BlackList> items= (List<BlackList>)pageResult.getItems();
        //非空判断
        if (!CollectionUtils.isEmpty(items)){
            //获取每个黑名单用户id
            List<Long> blackUserIds = items.stream().map(BlackList::getBlackUserId).collect(Collectors.toList());
            List<UserInfo> userInfoList = userInfoApi.findByBatchIds(blackUserIds);
            List<UserInfoVoAge> voList = new ArrayList<>();
            // 如果userInfo为空值 报source为null的问题
            Map<Long, UserInfo> userMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, u -> u));
            for (BlackList blackList : items) {
                // 黑名单人的id
                Long blackUserId = blackList.getBlackUserId();
                // 通过黑名单人的id 获取黑名单人的信息
                UserInfo userInfo = userMap.get(blackUserId);
                // 转成vo
                UserInfoVoAge vo = new UserInfoVoAge();
                BeanUtils.copyProperties(userInfo, vo);
                voList.add(vo);
            }

            pageResult.setItems(voList);
        }
        //9. 返回
        return pageResult;
        }
    /**
     * 移除黑名单
     * @param blackUserId
     */
    public void removeBlackList(Long blackUserId) {
        // 获取登陆用户id
        Long loginUserId = UserHolder.getUserId();
        // 调用api移除
        blackListApi.remove(loginUserId, blackUserId);
    }

    /**
     * 修改陌生人问题
     * @param paramMap
     */
    public void updateQuestion(Map<String, String> paramMap) {
        Question question = new Question();
        question.setTxt(paramMap.get("content"));
        question.setUserId(UserHolder.getUserId());
        questionApi.updateQuestion(question);
    }

}
